package com.tag;


import com.common.BeanContainer;
import com.common.CollectionUtil;
import com.common.ThreadLocalVarUtil;
import com.common.WebContextHelper;
import com.entity.Manager;
import com.entity.Source;
import com.facade.source.ISourceFacadeR;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.List;

public class AuthorityTag extends TagSupport {

    private String url;

    @Override
    public int doStartTag() throws JspException {
        if (StringUtils.isBlank(url)) {
            return EVAL_BODY_INCLUDE;
        }
        Boolean canAccess = (Boolean) ThreadLocalVarUtil.get(url);
        if (null == canAccess) {
            ISourceFacadeR sourceFacadeR = (ISourceFacadeR) BeanContainer.getBean("sourceFacadeR");
            String[] sourceUrlArray = url.split(",");
            Manager manager = WebContextHelper.getOnLineUser();
            /*
             *根据登录用户和指定资源链接查询该用户是有拥有该资源的访问权限
             */
            List<Source> userSourceList = sourceFacadeR.findByUserAndSourceUrlsLink(manager.getId(), sourceUrlArray);
            if (CollectionUtil.isEmpty(userSourceList)) {
                canAccess = Boolean.FALSE;
            } else {
                canAccess = Boolean.TRUE;
            }
            ThreadLocalVarUtil.put(url, canAccess);
        }
        if (canAccess) {
            return EVAL_BODY_INCLUDE;
        }
        return SKIP_BODY;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}